package com.gitee.fmadmin.controller.product;

import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.LambdaQuery;
import com.gitee.fmadmin.common.req.IdParam;
import com.gitee.fmadmin.common.resp.Result;
import com.gitee.fmadmin.common.util.CopyUtil;
import com.gitee.fmadmin.controller.product.param.ProductInfoParam;
import com.gitee.fmadmin.controller.product.vo.ProductInfoVO;
import com.gitee.fmadmin.dao.entity.ProductInfo;
import com.gitee.fmadmin.service.product.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 六如
 */
@RestController
@RequestMapping("product")
public class ProductInfoController {

    @Autowired
    private ProductInfoService productInfoService;

    /**
     * 分页查询
     *
     * @param param 查询参数
     * @return 返回分页结果
     */
    @GetMapping("/page")
    public Result<PageInfo<ProductInfoVO>> page(ProductInfoParam param) {
        LambdaQuery<ProductInfo> query = param.toLambdaQuery(ProductInfo.class);
        PageInfo<ProductInfo> pageInfo = productInfoService.doPage(query);
        PageInfo<ProductInfoVO> ret = pageInfo.convert(productInfo -> CopyUtil.copyBean(productInfo, ProductInfoVO::new));
        return Result.ok(ret);
    }

    /**
     * 新增记录
     *
     * @param productInfo 表单参数
     * @return 返回添加后的主键值
     */
    @PostMapping("/add")
    public Result<Long> add(@Validated @RequestBody ProductInfo productInfo) {
        productInfoService.save(productInfo);
        // 返回添加后的主键值
        return Result.ok(productInfo.getId());
    }

    /**
     * 修改记录
     *
     * @param productInfo 表单数据
     * @return 返回影响行数
     */
    @PostMapping("/update")
    public Result<Integer> update(@Validated @RequestBody ProductInfo productInfo) {
        return Result.ok(productInfoService.update(productInfo));
    }

    /**
     * 删除记录
     *
     * @param param 参数
     * @return 返回影响行数
     */
    @PostMapping("/delete")
    public Result<Integer> delete(@Validated @RequestBody IdParam param) {
        return Result.ok(productInfoService.deleteById(param.getId()));
    }


}
